<?php
class ConfigurationComponent extends Component {
	public $components = array('Session');
	public $controller = null;
	public $domain_id = null;
	private $prefix = 'cfg';

	public function initialize(Controller $controller) {
		$this->controller = $controller;
	}

	public function load(){
		
		$this->controller->loadModel('Configuration');
		$this->controller->Configuration->recursive = -1;
		$this->controller->loadModel('Upload');
		$this->controller->Upload->recursive = -1;
		
		$this->domain_id = $this->Session->read('Config.domain_id');
		if(is_null($this->domain_id)){
			$this->controller->loadModel('Domain');
			$domain = $this->controller->Domain->get_info(Configure::read('Config.domain'));
			$this->Session->write('Config.domain_id',$domain['Domain']['id']);
			$this->domain_id = $this->Session->read('Config.domain_id');
		}
		
		
		$options['joins']= array(
			array('table' => 'uploads',
		        'alias' => 'Upload',
		        'type' => 'left',
		        'conditions' => array(
		            'Configuration.upload_id = Upload.id'
				)
		));	
		$options['fields'] = array('Configuration.name','Configuration.value','Upload.name','Upload.description');
		$options['conditions'] = array( 'Configuration.domain_id' => $this->domain_id );
		$settings = $this->controller->Configuration->find('all',$options);
		foreach ($settings as $variable){
			Configure::write("$this->prefix.{$variable['Configuration']['name']}",$variable['Configuration']['value']);
			if(isset($variable['Upload']['name']) && !is_null($variable['Upload']['name'])){
// 				$this->Uploads->upload_file(Configure::read('Config.panel_uploads_folder').$variable['Upload']['name'], Configure::read('Config.loacl_uploads_folder'), $variable['Upload']['name'], false);
				$upload_id = $variable['Configuration']['name']."_upload";				
				Configure::write("$this->prefix.{$upload_id}",$variable['Upload']['name']);
				
				$upload_id = $variable['Configuration']['name']."_description";
				Configure::write("$this->prefix.{$upload_id}",$variable['Upload']['description']);
				
			}
		}
		
		$this->load_main();
	}
	
	private function load_main(){
	
		$this->controller->loadModel('Configuration');
		$this->controller->Configuration->recursive = -1;
		$this->controller->loadModel('Upload');
		$this->controller->Upload->recursive = -1;
	
		$options['joins']= array(
		array('table' => 'uploads',
			        'alias' => 'Upload',
			        'type' => 'left',
			        'conditions' => array(
			            'Configuration.upload_id = Upload.id'
		)
		));
		$options['fields'] = array('Configuration.name','Configuration.value','Upload.name');
		$options['conditions'] = array( 'Configuration.domain_id' => 0 );
		$settings = $this->controller->Configuration->find('all',$options);
		foreach ($settings as $variable){
			Configure::write("$this->prefix.{$variable['Configuration']['name']}",$variable['Configuration']['value']);
			if(isset($variable['Upload']['name']) && !is_null($variable['Upload']['name'])){
// 				$this->Uploads->upload_file(Configure::read('Config.panel_uploads_folder').$variable['Upload']['name'], Configure::read('Config.loacl_uploads_folder'), $variable['Upload']['name'], false);
				$upload_id = $variable['Configuration']['name']."_upload";
				Configure::write("$this->prefix.{$upload_id}",$variable['Upload']['name']);
			}
		}
	}
	
}